package com.example.demo.controller;

import com.example.demo.common.domain.User;
import com.example.demo.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class AdminController {

    @Autowired
    private AdminService adminService;
    @RequestMapping("/toIndex")
    public String ToIndex()
    {
        return "index";
    }
    @RequestMapping("/Signout")
    public String Signout(HttpSession session)
    {
        System.out.println(session.getAttribute("LoginUser"));
        session.setAttribute("LoginUser","");
        return "index";
    }

    @RequestMapping("/login")
    @ResponseBody
    public String Login(User user , Model model, HttpSession session){

        String  Uname=user.getuName();
        String  Upw=user.getuPwd();
        System.out.println("用户名+密码+权限:"+Uname+";"+Upw);
        String message=adminService.login(user);
        System.out.println(message);
        session.setAttribute("RoleMessage","");

        if (message.equals("正确")){
            session.setAttribute("LoginUser",Uname);
            return message;
        }else {
            model.addAttribute("loginMessage",message);
            return message;
        }
    }
}
